package com.piggy.sys.service.base.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.piggy.sys.api.vo.BaseAddressVo;
import com.piggy.sys.domain.base.BaseDistrict;
import com.piggy.sys.mapper.base.BaseDistrictMapper;
import com.piggy.sys.service.base.IBaseDistrictService;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 区县
 */
@Service
public class BaseDistrictServiceImpl extends ServiceImpl<BaseDistrictMapper, BaseDistrict> implements IBaseDistrictService {

    @Override
    public List<BaseAddressVo> getDistrict(String parentId) {
        List<BaseDistrict> baseDistrictList = baseMapper.selectList(Wrappers.<BaseDistrict>lambdaQuery().eq(BaseDistrict::getPid, parentId));
        return BeanUtil.copyToList(baseDistrictList, BaseAddressVo.class);
    }

    @Override
    public BaseAddressVo getDistrictVo(String districtId) {
        return baseMapper.getDistrictVo(districtId);
    }

    @Override
    public Map<String, String> getDistrictMap(List<String> districtIds) {
        if (CollUtil.isEmpty(districtIds)) {
            return Maps.newHashMap();
        }
        List<BaseAddressVo> baseAddressVos = baseMapper.getDistrictMap(districtIds);
        //return baseAddressVos.stream().collect(Collectors.toMap(item -> item.getDistId().toString(), BaseAddressVo::getDistName, (a, b) -> a));
        return baseAddressVos.stream().collect(LinkedHashMap::new, (m, v) -> m.put(v.getDistId().toString(), v.getDistName()), LinkedHashMap::putAll);
    }
}
